package com.honey.compilation.sql.ddl;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.sql.SqlColumn;
import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.compilation.sql.SqlTable;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;

/**
 * 添加Column. SQL 92中定义如下:<br />
 * &lt;alter table statement&gt;    ::=   ALTER TABLE &lt;table name&gt; &lt;alter table action&gt;<br />
 * &lt;alter table action&gt;    ::= &lt;add column definition&gt;<br />
 * &lt;add column definition&gt;    ::=   ADD [ COLUMN ] &lt;column definition&gt;<br />
 * &lt;column definition&gt;    ::= &lt;column name&gt; { &lt;data type&gt; | &lt;domain name&gt; } [ &lt;default clause&gt; ] [ &lt;column constraint definition&gt; ... ] [ &lt;collate clause&gt; ]
 * 
 * @author Administrator
 *
 */
public  class AlterTableAddColumn extends AlterTableAdd implements SqlScript{
	
	/** 列 */
	private List<SqlColumn> columns ;

	/**
	 * 构造函数
	 * @param tableName 表的名称
	 */
	public AlterTableAddColumn(String tableName) {
		super(tableName);
		columns=new ArrayList<SqlColumn>();
	}
	
	/**
	 * 构造函数
	 * @param table 数据库表对象
	 */
	public AlterTableAddColumn(SqlTable table){
		super(table);
		columns=new ArrayList<SqlColumn>();
	}

	/**
	 * 构造函数
	 * @param schema 数据库的schema
	 * @param tableName 表的名称
	 */
	public AlterTableAddColumn(String schema ,String tableName) {
		super(schema,tableName);
		columns=new ArrayList<SqlColumn>();
	}
	
	/**
	 * 构造函数
	 * @param tableName 表的名称
	 * @param columns 列
	 */
	public AlterTableAddColumn(String tableName, SqlColumn ...columns) {
		this(tableName);
		addColumn(columns);
	}
	
	/**
	 * 构造函数
	 * @param schema 数据库的schema
	 * @param tableName 表的名称
	 * @param columns 列
	 */
	public AlterTableAddColumn(String schema ,String tableName, SqlColumn ...columns) {
		this(schema,tableName);
		addColumn(columns);
	}
	
	
	/**
	 * 添加列
	 * @param columns
	 */
	public void addColumn(SqlColumn ...columns){
		for(SqlColumn column : columns ){
			if(column != null ){
				if(column instanceof ExtendConstraintColumn ){
					((ExtendConstraintColumn)column).setConstraintInline(true);
				}
				this.columns.add(column);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.sql.ddl.AlterTableAdd#compiledContent(int)
	 */
	@Override
	public CharacterBuilder compiledContent(int indentLevel) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder() ;
		
		boolean newLine = false;
		for(SqlColumn column : columns){
			if (newLine)IndentSpace.newLine(answer);else newLine = true;
			
			answer.append( super.compiledContent(indentLevel) )
			.append( SqlKeyWord.COLUMN )
			.append( column.compiledContent(indentLevel+1) )
			.append(SqlKeyWord.OPERATION_SEMICOLON);
			
		}
		
		return answer;
	}
}
